In [1]:
nums = [1, 2, 3, 4, 5]
s = sum(x * x for x in nums)

In [45]:
def dedupe(items, key=None):
    seen = set()
    for item in items:
        val = item if key is None else key(item)
        if val not in seen:
            yield item
            seen.add(val)

a = [ {'x':1, 'y':2}, {'x':1, 'y':3}, {'x':1, 'y':2}, {'x':2, 'y':4}]

nu = dedupe(a, key=lambda d: (d['x'],d['y']))
list(nu)


Out[45]:
[{'x': 1, 'y': 2}, {'x': 1, 'y': 3}, {'x': 2, 'y': 4}]

In [46]:
from collections import deque

class linehistory:
    def __init__(self, lines, histlen=3):
        self.lines = lines
        self.history = deque(maxlen=histlen)

    def __iter__(self):
        for line_index, line in enumerate(self.lines, 1):
            self.history.append((line_index, line))
            yield line

    def clear(self):
        self.history.clear()

        
with open('D:\\sql\\test.txt') as f:
    lines = linehistory(f)
    print(type(lines))
    for line in lines:
        if '# -' in line:
            for lineno, hline in lines.history:
                print('{}:{}'.format(lineno, hline), end='')


<class '__main__.linehistory'>
1:# ---------------
5:FROM `i_od_parcel_landside` AS p
6:GROUP BY p.`parcel_type`;
7:# ---------------
11:FROM `i_od_parcel_airside` AS p
12:GROUP BY p.`parcel_type`
13:# ---------------

Header

second header

Ordinary text

  • first point
    • second order
$$ x_1^{[1]} $$
var x = 1;

function x(){
    if (){}
}

yield from


In [106]:
def frange(start, stop, increment):
    x = start
    while x < stop:
        yield from add_run(x)
        x += increment

def add_run(x):
    yield x+x
    
f = iter(frange(1,9,1))

In [108]:
from itertools import islice
[x  for x in islice(f, 1)]


Out[108]:
[4]

In [102]:
# fron logging
class Node(object):
    def __init__(self, value):
        self._value = value
        self._children = []
    
    def __repr__(self):
        return 'Node({!r})'.format(self._value)
    
    def add_child(self, node):
        self._children.append(node)
        
    def __iter__(self):
        return iter(self._children)
    
    def depth_first(self):
        yield self
        for s in self:
            yield from s.depth_first()

In [103]:
root = Node(0)
child_1 = Node(1)
child_2 =Node(2)
root.add_child(child_1)
root.add_child(child_2)
# iter(root)
for c in root.depth_first():
    print(c)


Node(0)
Node(1)
Node(2)

In [104]:
import logging as Log
Log.basicConfig(format='%(asctime)s %(message)s', level=Log.INFO)


class Cont(object):
    def __init__(self, start, end):
        self._start = start
        self._end = end
        
    def __iter__(self):
        self.center = self._start
#         Log.info('{}'.format(self._start))
#         while n < self._end:
#             yield n
#             n += 1
        return self
    
    def __next__(self):
        if self.center == self._end:
            raise StopIteration
        self.center += 1
        return self.center
            
    def __reversed__(self):
        n = self._end
        while n >= self._start:
            yield n
            n -= 1

In [105]:
for rr in Cont(3,9):
    Log.info('info: {}'.format(rr))


2017-09-08 08:50:40,851 info: 4
2017-09-08 08:50:40,855 info: 5
2017-09-08 08:50:40,864 info: 6
2017-09-08 08:50:40,866 info: 7
2017-09-08 08:50:40,868 info: 8
2017-09-08 08:50:40,870 info: 9

In [170]:
class squares:
    def __init__(self, start, stop):
        self.flag = start - 1
        self.value = self.flag
        self.stop = stop
    def __iter__(self):
#         self.value = self.flag
        return self
    def __next__(self):
        if self.value == self.stop:
            raise StopIteration
        self.value += 1
        return self.value
    

a = squares(1,5)
b = squares(1,5)

# s = 0
# while s<=41:
#     for i in a:
#         s= s + i
#         print(s)

In [4]:
from collections import Iterable

def flatten(items, ignore_types=(str, bytes)):
    for x in items:
        if isinstance(x, Iterable) and not isinstance(x, ignore_types):
            yield from flatten(x)
        else:
            yield x

items = [1, 2, 8]
# Produces 1 2 3 4 5 6 7 8
for x in flatten(items):
    print(x)


1
2
8

In [4]:
from collections import deque

class LineHistory:
    def __init__(self, lines, histlen=1):
        self.lines = lines
        self.history = deque(maxlen=histlen)

    def __iter__(self):
        for lineno, line in enumerate(self.lines, 1):
            self.history.append((lineno, line))
#            记录每次的 
            yield line

    def clear(self):
        self.history.clear()
        
        
with open('D:\\sql\\test.txt') as f:
    line_history = LineHistory(f)
    for line in line_history:
        if 'SELECT' in line:
            
            for lineno, hline in line_history.history:
                print('{}:{}'.format(lineno, hline), end='')


2:SELECT
8:SELECT
14:SELECT
18:SELECT
22:SELECT
26:SELECT
30:SELECT

In [9]:
def gg(a,b):
    
    a, b=a, b
    n = 0
    while n<end:
        a, b= b, a+b
        yield a
        n += 1
[x for x in gg(10)]


def sanjiao(end):
    
    yield from


Out[9]:
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

In [ ]: